<template>
  <div style="padding: 20px;height: 670px">
    <t-card style="height: 670px">
      <t-card shadow="never" style="width: 1320px">
        <el-form :model="buildingQO" ref="buildingQO" :inline="true">
          <el-form-item label="楼栋单元" prop="buildingId" label-width="80px" style="width: 300px;padding-left: 200px">
            <el-select v-model="buildingQO.buildingId" filterable clearable placeholder="请选择楼栋单元"
                       style="width: 200px;">
              <el-option v-for="b in allBuildings" :key="b.buildingId"
                         :label="b.buildingName+b.buildingUnit" :value="b.buildingId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="楼栋状态" prop="buildingStatus" label-width="80px" style="width: 300px">
            <el-select v-model="buildingQO.buildingStatus" filterable clearable placeholder="请选择状态"
                       style="width: 200px;">
              <el-option v-for="s in status" :key="s.value" :label="s.label" :value="s.value">
              </el-option>
            </el-select>
          </el-form-item>
          <t-button theme="primary" variant="dashed" size="large" @click="select(1)">查询</t-button>
          &ensp;
          <t-button theme="primary" variant="outline" size="large"
                    @click="resetForm('buildingQO')">重置
          </t-button>
          &ensp;
          <t-button theme="primary" size="large" @click="dialogAdd=true">添加</t-button>
        </el-form>
      </t-card>
      <br/>
      <t-card shadow="never" style="width: 1320px">
        <el-table :data="currentBuildings" border style="height:425px;">
          <el-table-column type="index" align="center"></el-table-column>
          <el-table-column prop="buildingName" label="楼栋" align="center"></el-table-column>
          <el-table-column prop="buildingUnit" label="单元" align="center"></el-table-column>
          <el-table-column prop="buildingStatus" label="楼栋状态" align="center">
            <template slot-scope="scope">
              <span v-if="scope.row.buildingStatus == 0">未开放</span>
              <span v-if="scope.row.buildingStatus == 1">已开放</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-row>
                <t-button theme="primary" circle plain @click="edit(scope)">编辑</t-button>
                &ensp;
                <t-button theme="primary" variant="outline"  circle plain
                          @click="del(scope)">删除</t-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
        <br/>
        <el-pagination background layout="prev, pager, next" :current-page.sync="currentPage"
                       :page-count="totalPage" @current-change="goPage">
        </el-pagination>
      </t-card>

      <el-dialog title="添加楼栋信息" :visible.sync="dialogAdd" :width="400">
        <el-form :model="addBuilding" ref="addBuilding">
          <el-row>
            <el-col>
              <el-form-item label="楼栋" prop="buildingName" label-width="50px">
                <el-input v-model="addBuilding.buildingName" filterable clearable
                          placeholder="请输入楼栋"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="单元" prop="buildingUnit" label-width="50px">
                <el-input v-model="addBuilding.buildingUnit" filterable clearable
                          placeholder="请输入单元"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="状态" prop="buildingStatus" label-width="50px">
                <el-select v-model="addBuilding.buildingStatus" disabled filterable clearable
                           placeholder="未开放">
                  <!-- <el-option v-for="s in status" :key="s.value" :label="s.label" :value="s.value">
                  </el-option> -->
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <t-button theme="primary" variant="outline" @click="dialogAdd=false,resetForm('addBuilding')">取 消</t-button>
          &ensp;
          <t-button theme="primary" @click="add">确 定</t-button>
        </div>
      </el-dialog>

      <el-dialog title="更新楼栋信息" :visible.sync="dialogUpdate">
        <el-form :model="updateBuilding" ref="updateBuilding">
          <el-row>
            <el-col :span="10" :offset="0">
              <el-form-item label="楼栋" prop="buildingName" label-width="80px">
                <el-input v-model="updateBuilding.buildingName" filterable clearable
                          placeholder="请输入楼栋"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="0">
              <el-form-item label="单元" prop="buildingUnit" label-width="80px">
                <el-input v-model="updateBuilding.buildingUnit" filterable clearable
                          placeholder="请输入单元"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10" :offset="0">
              <el-form-item label="状态" prop="buildingStatus" label-width="80px">
                <el-select v-model="updateBuilding.buildingStatus" filterable clearable placeholder="请选择状态">
                  <el-option v-for="s in status" :key="s.value" :label="s.label" :value="s.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <t-button theme="primary" variant="outline" @click="dialogUpdate=false">取 消</t-button>
          &ensp;
          <t-button theme="primary" @click="update">确 定</t-button>
        </div>
      </el-dialog>
    </t-card>
  </div>
</template>

<script>
import axios from "axios"
import {MessagePlugin} from "tdesign-vue";

export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "buildings",
  data() {
    return {
      buildingQO: {},
      allBuildings: [],
      currentBuildings: [],

      dialogAdd: false,
      dialogUpdate: false,
      addBuilding: {},
      updateBuilding: {},
      status: [{
        value: '0',
        label: '未开放'
      }, {
        value: '1',
        label: '已开放'
      }],
      totalPage: 0,
      currentPage: 0,
    }
  },
  methods: {
    del(scope) {
      this.$confirm('此操作将永久删除楼栋信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        axios.get("/api/admin/building/del", {
          params: {
            id: scope.row.buildingId
          }
        }).then(res => {
          if (res.data.code == 200) {
            this.currentBuildings.splice(scope.$index, 1);
            this.allBuildings.splice(scope.$index, 1);
            this.getAll();
            MessagePlugin.success('删除成功')
          } else {
            MessagePlugin.error('楼栋已开放，不能删除')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    edit(scope) {
      this.dialogUpdate = true;
      //数据回显
      this.updateBuilding = {
        ...scope.row
      }
    },
    update() {
      if (this.updateBuilding.buildingId) {
        axios.post("/api/admin/building/update", this.updateBuilding)
            .then(res => {
              if (res.data.code == 200) {
                this.dialogUpdate = false;
                this.goPage(this.currentPage);
                MessagePlugin.success('成功')
              }
            })
      }
    },
    add() {
      axios.post("/api/admin/building/add", this.addBuilding)
          .then(res => {
            if (res.data.code == 200) {
              this.dialogAdd = false;
              this.getAll();
              this.goPage(this.currentPage);
              MessagePlugin.success('成功')
            }
            this.resetForm('addBuilding');
          });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    select(p) {
      axios.get("/api/admin/building/list/" + p, {
        params: this.buildingQO
      }).then(res => {
        if (res.data.code == 200) {
          this.currentBuildings = res.data.data.list;
          this.totalPage = res.data.data.pages;
          this.currentPage = 1;
          MessagePlugin.success('成功')
        }
      })
    },
    goPage(p) {
      axios.get("/api/admin/building/list/" + p, {
        params: this.buildingQO
      }).then(res => {
        if (res.data.code == 200) {
          this.currentBuildings = res.data.data.list;
          this.totalPage = res.data.data.pages;
        }
      })
    },
    getAll() {
      axios.get("/api/admin/building/getAll").then(res => {
        if (res.data.code == 200) {
          this.allBuildings = res.data.data;
        }
      })
    },
  },
  mounted() {
    this.getAll();
    this.goPage(1);
  }
}
</script>

<style>

</style>